package com.aurora.controller;

import cn.hutool.core.thread.ThreadUtil;
import com.aurora.model.vo.Result;
import com.aurora.model.vo.rabbitMQ.FanoutSender;
import com.aurora.model.vo.rabbitMQ.MqttBody;
import com.aurora.model.vo.rabbitMQ.SimpleSender;
import com.aurora.model.vo.rabbitMQ.WorkSender;
import com.aurora.service.MqttService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Administrator
 */
@Api(tags = "RabbitMqController")
@RestController
@RequestMapping("/rabbitmq")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class RabbitMqController {

    private final SimpleSender simpleSender;
    private final WorkSender workSender;
    private final FanoutSender fanoutSender;
    private final MqttService mqttService;




    /**
     * 查看在线列表
     *
     * @return allUserList list<userDto></userDto>
     */
    @ApiOperation(value = "查看在线列表")
    @GetMapping("/getAllLoginUser")
    public Result<?> getAllUserList() {
        return Result.ok(mqttService.getAllLoginUser());
    }

    /**
     * 聊天室
     *
     * @param mqttBody 消息体
     */
    @PostMapping("/sendToDefaultTopic")
    @ApiOperation("向默认主题发送消息")
    public Result<?> sendToDefaultTopic(@RequestBody MqttBody mqttBody) {

        return mqttService.sendMessage(mqttBody);
    }


    /**
     * 对话
     *
     * @param mqttBody 消息实体
     */
    @PostMapping("/sendToTopic")
    @ApiOperation("向指定主题发送消息")
    public Result<?> sendToTopic(@RequestBody MqttBody mqttBody) {
        return mqttService.sendMessage(mqttBody);
    }

    /**
     * 简单模式
     *
     * @param message 消息
     */
    @ApiOperation(value = "简单模式")
    @GetMapping("/simple")
    public Result<?> simple(String message) {
        //循环20次
        for (int i = 0; i < 20; i++) {
            simpleSender.send(message);
            //休眠1秒
            ThreadUtil.sleep(1000);
        }
        return Result.ok();
    }

    @ApiOperation("工作模式")
    @RequestMapping(value = "/work", method = RequestMethod.GET)
    public Result<?> workTest() {
        for (int i = 0; i < 10; i++) {
            workSender.send(i);
            ThreadUtil.sleep(1000);
        }
        return Result.ok();
    }


    @ApiOperation("发布/订阅模式")
    @RequestMapping(value = "/fanout", method = RequestMethod.GET)
    public Result<?> fanoutTest() {
        for (int i = 0; i < 10; i++) {
            fanoutSender.send(i);
            ThreadUtil.sleep(1000);
        }
        return Result.ok(null);
    }


}
